<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Serializer Stream Operations</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Beast">
<link rel="up" href="../using_http.html" title="HTTP">
<link rel="prev" href="message_stream_operations.html" title="Message Stream Operations">
<link rel="next" href="parser_stream_operations.html" title="Parser Stream Operations">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="message_stream_operations.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_http.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="parser_stream_operations.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="beast.using_http.serializer_stream_operations"></a><a class="link" href="serializer_stream_operations.html" title="Serializer Stream Operations">Serializer
      Stream Operations</a>
</h3></div></div></div>
<p>
        Non-trivial algorithms need to do more than send entire messages at once,
        such as:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            Send the header first, and the body later.
          </li>
<li class="listitem">
            Send a message incrementally: bounded work in each I/O cycle.
          </li>
<li class="listitem">
            Use a series of caller-provided buffers to represent the body.
          </li>
</ul></div>
<p>
        These tasks may be performed by using the serializer stream interfaces. To
        use these interfaces, first construct an appropriate serializer from the
        message to be sent:
      </p>
<div class="table">
<a name="beast.using_http.serializer_stream_operations.serializer"></a><p class="title"><b>Table 1.20. Serializer</b></p>
<div class="table-contents"><table class="table" summary="Serializer">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Name
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__http__serializer.html" title="http::serializer"><code class="computeroutput"><span class="identifier">serializer</span></code></a>
                </p>
              </td>
<td>
<pre class="programlisting"><span class="comment">/// Provides buffer oriented HTTP message serialization functionality.</span>
<span class="keyword">template</span><span class="special">&lt;</span>
    <span class="keyword">bool</span> <span class="identifier">isRequest</span><span class="special">,</span>
    <span class="keyword">class</span> <span class="identifier">Body</span><span class="special">,</span>
    <span class="keyword">class</span> <span class="identifier">Fields</span> <span class="special">=</span> <span class="identifier">fields</span>
<span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">serializer</span><span class="special">;</span>
</pre>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__http__request_serializer.html" title="http::request_serializer"><code class="computeroutput"><span class="identifier">request_serializer</span></code></a>
                </p>
              </td>
<td>
<pre class="programlisting"><span class="comment">/// A serializer for HTTP/1 requests</span>
<span class="keyword">template</span><span class="special">&lt;</span>
    <span class="keyword">class</span> <span class="identifier">Body</span><span class="special">,</span>
    <span class="keyword">class</span> <span class="identifier">Fields</span> <span class="special">=</span> <span class="identifier">fields</span>
<span class="special">&gt;</span>
<span class="keyword">using</span> <span class="identifier">request_serializer</span> <span class="special">=</span> <span class="identifier">serializer</span><span class="special">&lt;</span><span class="keyword">true</span><span class="special">,</span> <span class="identifier">Body</span><span class="special">,</span> <span class="identifier">Fields</span><span class="special">&gt;;</span>
</pre>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__http__response_serializer.html" title="http::response_serializer"><code class="computeroutput"><span class="identifier">response_serializer</span></code></a>
                </p>
              </td>
<td>
<pre class="programlisting"><span class="comment">/// A serializer for HTTP/1 responses</span>
<span class="keyword">template</span><span class="special">&lt;</span>
    <span class="keyword">class</span> <span class="identifier">Body</span><span class="special">,</span>
    <span class="keyword">class</span> <span class="identifier">Fields</span> <span class="special">=</span> <span class="identifier">fields</span>
<span class="special">&gt;</span>
<span class="keyword">using</span> <span class="identifier">response_serializer</span> <span class="special">=</span> <span class="identifier">serializer</span><span class="special">&lt;</span><span class="keyword">false</span><span class="special">,</span> <span class="identifier">Body</span><span class="special">,</span> <span class="identifier">Fields</span><span class="special">&gt;;</span>
</pre>
              </td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
        The choices for template types must match the message passed on construction.
        This code creates an HTTP response and the corresponding serializer:
      </p>
<pre class="programlisting"><span class="identifier">response</span><span class="special">&lt;</span><span class="identifier">string_body</span><span class="special">&gt;</span> <span class="identifier">res</span><span class="special">;</span>

<span class="identifier">response_serializer</span><span class="special">&lt;</span><span class="identifier">string_body</span><span class="special">&gt;</span> <span class="identifier">sr</span><span class="special">{</span><span class="identifier">res</span><span class="special">};</span>
</pre>
<p>
        The stream operations which work on serializers are:
      </p>
<div class="table">
<a name="beast.using_http.serializer_stream_operations.serializer_stream_operations"></a><p class="title"><b>Table 1.21. Serializer Stream Operations</b></p>
<div class="table-contents"><table class="table" summary="Serializer Stream Operations">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Name
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__http__write/overload1.html" title="http::write (1 of 6 overloads)"><span class="bold"><strong>write</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Send everything in a <a class="link" href="../ref/boost__beast__http__serializer.html" title="http::serializer"><code class="computeroutput"><span class="identifier">serializer</span></code></a> to a <a href="../../../../../../doc/html/boost_asio/reference/SyncWriteStream.html" target="_top"><span class="emphasis"><em>SyncWriteStream</em></span></a>.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__http__async_write/overload1.html" title="http::async_write (1 of 3 overloads)"><span class="bold"><strong>async_write</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Send everything in a <a class="link" href="../ref/boost__beast__http__serializer.html" title="http::serializer"><code class="computeroutput"><span class="identifier">serializer</span></code></a> asynchronously
                  to an <a href="../../../../../../doc/html/boost_asio/reference/AsyncWriteStream.html" target="_top"><span class="emphasis"><em>AsyncWriteStream</em></span></a>.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__http__write_header/overload1.html" title="http::write_header (1 of 2 overloads)"><span class="bold"><strong>write_header</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Send only the header from a <a class="link" href="../ref/boost__beast__http__serializer.html" title="http::serializer"><code class="computeroutput"><span class="identifier">serializer</span></code></a> to a <a href="../../../../../../doc/html/boost_asio/reference/SyncWriteStream.html" target="_top"><span class="emphasis"><em>SyncWriteStream</em></span></a>.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__http__async_write_header.html" title="http::async_write_header"><span class="bold"><strong>async_write_header</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Send only the header from a <a class="link" href="../ref/boost__beast__http__serializer.html" title="http::serializer"><code class="computeroutput"><span class="identifier">serializer</span></code></a> asynchronously
                  to an <a href="../../../../../../doc/html/boost_asio/reference/AsyncWriteStream.html" target="_top"><span class="emphasis"><em>AsyncWriteStream</em></span></a>.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__http__write_some/overload1.html" title="http::write_some (1 of 2 overloads)"><span class="bold"><strong>write_some</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Send part of a <a class="link" href="../ref/boost__beast__http__serializer.html" title="http::serializer"><code class="computeroutput"><span class="identifier">serializer</span></code></a> to a <a href="../../../../../../doc/html/boost_asio/reference/SyncWriteStream.html" target="_top"><span class="emphasis"><em>SyncWriteStream</em></span></a>.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__http__async_write_some.html" title="http::async_write_some"><span class="bold"><strong>async_write_some</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Send part of a <a class="link" href="../ref/boost__beast__http__serializer.html" title="http::serializer"><code class="computeroutput"><span class="identifier">serializer</span></code></a> asynchronously
                  to an <a href="../../../../../../doc/html/boost_asio/reference/AsyncWriteStream.html" target="_top"><span class="emphasis"><em>AsyncWriteStream</em></span></a>.
                </p>
              </td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
        Here is an example of using a serializer to send a message on a stream synchronously.
        This performs the same operation as calling <code class="computeroutput"><span class="identifier">write</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">m</span><span class="special">)</span></code>:
      </p>
<pre class="programlisting"><span class="comment">/** Send a message to a stream synchronously.

    @param stream The stream to write to. This type must support
    the &lt;em&gt;SyncWriteStream&lt;/em&gt; concept.

    @param m The message to send. The Body type must support
    the &lt;em&gt;BodyWriter&lt;/em&gt; concept.
*/</span>
<span class="keyword">template</span><span class="special">&lt;</span>
    <span class="keyword">class</span> <span class="identifier">SyncWriteStream</span><span class="special">,</span>
    <span class="keyword">bool</span> <span class="identifier">isRequest</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Body</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Fields</span><span class="special">&gt;</span>
<span class="keyword">void</span>
<span class="identifier">send</span><span class="special">(</span>
    <span class="identifier">SyncWriteStream</span><span class="special">&amp;</span> <span class="identifier">stream</span><span class="special">,</span>
    <span class="identifier">message</span><span class="special">&lt;</span><span class="identifier">isRequest</span><span class="special">,</span> <span class="identifier">Body</span><span class="special">,</span> <span class="identifier">Fields</span><span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">m</span><span class="special">)</span>
<span class="special">{</span>
    <span class="comment">// Check the template types</span>
    <span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">is_sync_write_stream</span><span class="special">&lt;</span><span class="identifier">SyncWriteStream</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">,</span>
        <span class="string">"SyncWriteStream type requirements not met"</span><span class="special">);</span>
    <span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">is_body_writer</span><span class="special">&lt;</span><span class="identifier">Body</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">,</span>
        <span class="string">"BodyWriter type requirements not met"</span><span class="special">);</span>

    <span class="comment">// Create the instance of serializer for the message</span>
    <span class="identifier">serializer</span><span class="special">&lt;</span><span class="identifier">isRequest</span><span class="special">,</span> <span class="identifier">Body</span><span class="special">,</span> <span class="identifier">Fields</span><span class="special">&gt;</span> <span class="identifier">sr</span><span class="special">{</span><span class="identifier">m</span><span class="special">};</span>

    <span class="comment">// Loop until the serializer is finished</span>
    <span class="keyword">do</span>
    <span class="special">{</span>
        <span class="comment">// This call guarantees it will make some</span>
        <span class="comment">// forward progress, or otherwise return an error.</span>
        <span class="identifier">write_some</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">sr</span><span class="special">);</span>
    <span class="special">}</span>
    <span class="keyword">while</span><span class="special">(!</span> <span class="identifier">sr</span><span class="special">.</span><span class="identifier">is_done</span><span class="special">());</span>
<span class="special">}</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie
      Falco<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="message_stream_operations.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_http.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="parser_stream_operations.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
